﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNetNuke.Entities.Users;
using XepientSolutions.XSDocumentLibrary;
using DotNetNuke.Security.Roles;
using System.Collections;
using DotNetNuke.Entities.Modules;
using System.Web;
using DotNetNuke.Entities.Portals;
using Aricie.Documents.Services.Pdf;
using Aricie.Documents.Services;

namespace Aricie.XepientOpenDocLuceneProvider.Components.Business
{
    public class XepientDocumentLibraryController
    {

        #region Public Methods

        public int GetHostUserId(int portalId)
        {
            int toReturn = -1;

            UserInfo user = UserController.GetUserByName(portalId, "host");
            if (user != null)
                toReturn = user.UserID;

            return toReturn;
        }

        public XSDocumentLibraryInfoFolder GetRootFolder(XSDocumentLibraryController docController, XSDocumentLibraryInfoFolder folder, int hostUserId, ref  List<XSDocumentLibraryInfoFolder> listTreeFolders)
        {
            XSDocumentLibraryInfoFolder toReturn = folder;

            listTreeFolders.Add(folder);

            if (toReturn.FolderParentId != -1)
            {
                XSDocumentLibraryInfoFolder folderParent = docController.SelectFolder(toReturn.FolderParentId, hostUserId);
                toReturn = GetRootFolder(docController, folderParent, hostUserId, ref listTreeFolders);
            }

            return toReturn;
        }

        public List<int> ListRoleIdsView(int portalId, XSDocumentLibraryController docController, XSDocumentLibraryInfoFolder xsDocFolder)
        {
            List<int> toReturn = new List<int>();

            RoleController rController = new RoleController();
            ArrayList listRoles = docController.SelectFolderDNNRoleAssocByFolderId(xsDocFolder.FolderId);
            if (listRoles != null)
            {
                foreach (XSDocumentLibraryInfoFolderDNNRole dnnRole in listRoles)
                {
                    if (dnnRole.View)
                    {
                        RoleInfo role = rController.GetRoleByName(portalId, dnnRole.DNNRoleName);
                        if (role != null)
                            toReturn.Add(role.RoleID);
                    }
                }
            }

            return toReturn;
        }

        public List<int> ListUserIdsView(int portalId, XSDocumentLibraryController docController, XSDocumentLibraryInfoFolder xsDocFolder)
        {
            List<int> toReturn = new List<int>();

            RoleController rController = new RoleController();
            ArrayList listUsers = docController.SelectFolderDNNUserAssocByFolderId(xsDocFolder.FolderId);
            if (listUsers != null)
            {
                foreach (XSDocumentLibraryInfoFolderDNNUser dnnUser in listUsers)
                {
                    if (dnnUser.View)
                        toReturn.Add(dnnUser.DNNUserId);
                }
            }
            return toReturn;
        }

        public string GetDownloadUrl(int portalId, ModuleInfo modInfo, int docId)
        {
            XSDocumentLibraryController ctrl = new XSDocumentLibraryController();
            //XSSecurity sec = new XSSecurity()

            string toReturn = string.Empty;
            //try
            //{
            //toReturn += "http://";
            //toReturn += portal.PortalAlias.HTTPAlias;
            toReturn += "/DesktopModules/XSDocumentLibrary/Components/FileDownloader/XSFileDownloaderPage.aspx?tabid=";
            toReturn += modInfo.TabID.ToString();
            toReturn += "&xsdid=";
            toReturn += docId.ToString();
            toReturn += "&xspid=";
            toReturn += portalId.ToString();
            toReturn += "&xslrf=";
            toReturn += "/DesktopModules/XSDocumentLibrary/App_LocalResources/XSDocumentLibrary";
            toReturn += "&xscl=";
            toReturn += "fr-FR";
            toReturn += "&xsmcs=";
            toReturn += HttpUtility.UrlEncode("/DesktopModules/XSDocumentLibrary/");
            toReturn += "&xsuarn=";
            toReturn += "Administrators";
            toReturn += "&xscd=";
            toReturn += "False";
            toReturn += "&xstmid=";
            toReturn += modInfo.TabModuleID.ToString();
            toReturn += "&xsift=1";
            //}
            //catch (Exception ex)
            //{
            //}

            return toReturn;
        }

        public string GetContentFromFile(string filePath)
        {
            return GetContentFromFile(filePath, -1);
        }

        public string GetContentFromFile(string filePath, int maxLengthChars)
        {
            string toReturn = string.Empty;

            DocumentFile docFile = new DocumentFile(filePath);
            toReturn = docFile.ReadDocumentText(maxLengthChars);

            return toReturn;
        }

        #endregion
    }
}
